use std::error::Error;

use super::{Argument, SocketState};

#[derive(Debug)]
pub struct Command<'a> {
    pub topic: Option<&'a str>,
    pub partitions: Option<u16>,
    pub from_beginning: Option<u16>,
    pub offset: Option<&'a str>,
    pub group: Option<&'a str>,
    pub replication_factor: Option<u16>,
}

impl<'a> Command<'a> {
    pub fn new(args: &'a [Argument]) -> Command<'_> {
        let mut command_topic = None;
        let mut command_partition = None;
        let mut command_begin = None;
        let mut command_offset = None;
        let mut command_group = None;
        let mut command_replication = None;
        for arg in args.iter() {
            match arg {
                Argument::Topic { topic } => command_topic = Some(topic.as_str()),
                Argument::Group { group } => command_group = Some(group.as_str()),
                Argument::Partitions { partitions } => command_partition = Some(*partitions),
                Argument::Offset { offset } => command_offset = Some(offset.as_str()),
                Argument::FromBeginning { from_beginning } => command_begin = Some(*from_beginning),
                Argument::ReplicationFactor { replication_factor } => {
                    command_replication = Some(*replication_factor)
                }
                _ => {}
            }
        }
        Command {
            topic: command_topic,
            partitions: command_partition,
            from_beginning: command_begin,
            offset: command_offset,
            group: command_group,
            replication_factor: command_replication,
        }
    }

    pub fn parse(&self, state: SocketState) -> Result<(), Box<dyn Error>> {
        Ok(())
    }
}
